package com.vmove.childtrain.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.vmove.childtrain.utils.LogUtils;

public class ScreenStatusReceiver extends BroadcastReceiver {
    String SCREEN_ON = "android.intent.action.SCREEN_ON";
    String SCREEN_OFF = Intent.ACTION_SCREEN_OFF;
    String POWER_OFF = Intent.ACTION_CLOSE_SYSTEM_DIALOGS;

    Context mContext;

    public ScreenStatusReceiver(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (SCREEN_OFF.equalsIgnoreCase(intent.getAction())){
            LogUtils.i("屏幕熄灭了");
            closeDriver();
            AppManager.getAppManager().AppExit(mContext);
        }
        if (POWER_OFF .equalsIgnoreCase(intent.getAction())){
            LogUtils.e("按下了电源键");
            closeDriver();
            AppManager.getAppManager().AppExit(mContext);
        }
    }

    private void closeDriver(){
        CustomSerialDriver instance = CustomSerialDriver.getInstance(AppContext.driver);
        if (instance.isConnect()){
            instance.closeSerial();
        }
    }
}
